﻿using System;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using EduFormManager.Models;

namespace EduFormManager.Forms.UserControls.MunicipalityFormulaPeek
{
    public partial class MunicipalityFormulaPeekControl : XtraUserControl
    {
        private municipality _municipality;
        private form _munitForm;
        private form _eduForm;
        private int _year;
        private mm_regular__summary_form _formula;

        public event EventHandler<MunicipalityFormulaPeekArgs> Completed;

        public MunicipalityFormulaPeekControl(Entities db, municipality munit, form munitForm, int year)
        {
            InitializeComponent();
            IsAvailable = false;

            Initialize(db, munit, munitForm, year);
        }

        async private void Initialize(Entities db, municipality munit, form munitForm, int year)
        {
            _formula = await db.mm_regular__summary_form.SingleOrDefaultAsync(t => t.summary_form_id == munitForm.form_id);
            this.IsAvailable = (_formula != null);
            if (this.IsAvailable)
            {
                _municipality = munit;
                _munitForm = munitForm;
                _eduForm = _formula.regular_form;
                _year = year;
                var eduList = await
                    db.edu_form_data.Where(t => t.form.form_id == _eduForm.form_id 
                        && t.send_date.Year == year && 
                        t.edu.municipality.municipality_id == munit.municipality_id)
                        .Select(t => t.edu)
                        .ToListAsync();
                foreach (var edu in eduList)
                {
                    Debug.WriteLine("{0} {1}", edu.edu_id, edu.name);
                }
                this.listBoxControlEdu.DataSource = eduList;
                this.listBoxControlEdu.DisplayMember = "name";
            }

            this.buttonCancel.Click += CompleteHandler;
            this.buttonOk.Click += CompleteHandler;
            this.checkEditCheckAll.CheckedChanged += (sender, args) =>
            {
                if (this.checkEditCheckAll.Checked)
                    this.listBoxControlEdu.CheckAll();
                else
                    this.listBoxControlEdu.UnCheckAll();
            };
        }

        public bool IsAvailable { get; private set; }

        private void CompleteHandler(object sender, EventArgs e)
        {
            var args = new MunicipalityFormulaPeekArgs
            {
                Write = this.IsAvailable
            };

            if (IsAvailable)
            {
                foreach (int index in this.listBoxControlEdu.CheckedIndices)
                {
                    var edu = (edu) this.listBoxControlEdu.GetItem(index);
                    var data = edu.edu_form_data.FirstOrDefault(t => t.form.form_id == _eduForm.form_id 
                        && t.send_date.Year == _year);
                    if (data == null) continue;
                    args.FormDataList.Add(data);
                }
                args.FormulaBytes = _formula.file_data;
            }
            OnCompleted(args);
        }

        private void listBoxControlEdu_GetItemEnabled(object sender, GetItemEnabledEventArgs e)
        {
            //var edu = this.listBoxControlEdu.GetItem(e.Index) as edu;
            //if (edu == null) return;
            //e.Enabled = edu.edu_form_data.Any(t => t.form == _eduForm && t.send_date.Year == _year);
        }

        protected virtual void OnCompleted(MunicipalityFormulaPeekArgs e)
        {
            var handler = Completed;
            if (handler != null) handler(this, e);
        }
    }
}
